//
//  NewVersionUtil.swift
//  ANOA
//
//  Created by 邱仙凯 on 2023/9/21.
//  Copyright © 2023 zwb. All rights reserved.
//  1 - 真用户模式切换
//  2 - LifeCycleTool  优化 logout startapp
//  3 - 登录页   面容登录按钮
//  4 - 设置中心 面容启用开关
//
//

import UIKit
import BiometricAuthentication
let OpenFaceIDKey = "OpenFaceIDKey"
let FaceIDUsername = "FaceIDUsername"
let FaceIDPassword = "FaceIDPassword"
let FaceIDFirstGuide = "FaceIDFirstGuide"

final class FaceIdTool {
   
    private init() {}
    
    static let shared = FaceIdTool()
    
    func openFaceIdAction(resultClo:@escaping((Bool,String)->())) {
     
     
       if BioMetricAuthenticator.shared.isFaceIdDevice() {
           BioMetricAuthenticator.authenticateWithBioMetrics(reason: "使用面容儿") { (result) in
               
               switch result {
               case .success( _):
                   
               resultClo(true,"面容登录启用成功")
                 
                   
               case .failure(let error):
                   
                   switch error {
                       
                       // device does not support biometric (face id or touch id) authentication
                   case .biometryNotAvailable:
                       alertUtil(title: "面容开启未成功,请检查是否允许面容ID", operateS: [nameAction(name: "好", action: { _ in
                           
                          let url = URL(string: UIApplication.openSettingsURLString)!
                          if UIApplication.shared.canOpenURL(url) {
                              UIApplication.shared.open(url)
                          }
                       })])
               
 
                       // No biometry enrolled in this device, ask user to register fingerprint or face
                   case .biometryNotEnrolled:
                       KSToast("面容未设置，请先设置面容")
                       
                       // show alternatives on fallback button clicked
                   case .fallback:
                       KSToast(error.message(),duration: 2)
                       // Biometry is locked out now, because there were too many failed attempts.
                       // Need to enter device passcode to unlock.
                   case .biometryLockedout:
                       KSToast(error.message(),duration: 2)
                       
                       // do nothing on canceled by system or user
                   case .canceledBySystem, .canceledByUser:
                       KSLog(error.message())
                       
                       // show error for any other reason
                   default:
                       if error.message().contains("set device passcode") {
                           KSToast("需要设置系统密码才能启用面容")
                       }
                    
                   }
                   resultClo(false,"")
               }
           }
        } else {
            resultClo(false,"")
            KSToast("设备不支持面容")
        }

        
        

        
        
        
        
        
        
    }
// 保存和清空  人脸登录内容
    func updateFaceIDStatus(flag:Bool) {
    
        let faceidkey = flag ? "1" : "0"
         UserDefaults.standard.set(faceidkey, forKey: OpenFaceIDKey)
        UserDefaults.standard.set(USERINFO.logid.unwrap, forKey: FaceIDUsername)
        UserDefaults.standard.set(USERINFO.usrPass.unwrap, forKey: FaceIDPassword)
   }



}


 
